/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 * 
 */
package seedpod.webapp.view.seedpodwidgets;

import seedpod.exceptions.InvalidWidgetException;
import seedpod.model.AVPair;
import seedpod.model.ProtegeValueType;
import seedpod.model.SeedpodModel.RdbCls.FormWidget;
import seedpod.model.SeedpodModel.RdbCls.ViewWidget;
import seedpod.model.rdb.RdbValueType;
import seedpod.webapp.view.htmlwidget.TextAreaInput;

public class TextArea extends LimsWidget {
	private String textData;
	private int _height, _width;

	public TextArea() {
	// TODO Auto-generated constructor stub
	}

	public TextArea(AVPair avpair) {
		super( avpair );
		setSize();
	}

	@Override
	public FormWidget getFormWidgetType() {
		return FormWidget.TEXTAREA;
	}

	@Override
	public ViewWidget getViewWidgetType() {
		return ViewWidget.STRING;
	}

	@Override
	public String render() {
		TextAreaInput taInput = new TextAreaInput( getAttribute().getName(), textData );
		taInput.setCssClass( "text" );
		taInput.setSize( _width, _height );
		return taInput.render();
	}

	private void setSize() {
		String[] param = getAttribute().getFormWidgetParam().split( ";" ); // getAttribute().getParameters();
		if ( param != null ) {
			_height = Integer.decode( param[0] ).intValue();
			_width = Integer.decode( param[1] ).intValue();
		} else {
			_height = TextAreaInput.DEFAULT_HEIGHT;
			_width = TextAreaInput.DEFAULT_WIDTH;
		}
	}

	@Override
	public void validateWidget() throws InvalidWidgetException {
		boolean valid = getAttribute().getRdbValueType() == RdbValueType.VARCHAR || getAttribute().getProtegeValueType().equals(
				ProtegeValueType.String ) || getAttribute().getRdbValueType().equals( RdbValueType.TEXT );
		if ( !valid ) throw new InvalidWidgetException( "Text area requires data value type of strings. " );
	}
}